<%- include('css') %>
    <div id="app" style="width:980px">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend>权限新建</legend>
        </fieldset>

        <form class="layui-form" action="" lay-filter="form">

            <div class="layui-form-item">
                <label class="layui-form-label">权限名</label>
                <div class="layui-input-block">
                    <input type="text" name="name" lay-verify="required|name" placeholder="请输入权限名" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                    <label class="layui-form-label">权限编号</label>
                    <div class="layui-input-block">
                        <input type="text" name="code" lay-verify="required|code" placeholder="请输入权限编号" autocomplete="off" class="layui-input">
                    </div>
                </div>
            <div class="layui-form-item">
                <label class="layui-form-label">权限路径</label>
                <div class="layui-input-inline">
                    <input type="text" name="path" lay-verify="required|path" placeholder="请输入权限路径" autocomplete="off" class="layui-input">
                </div>
                <div class="layui-form-mid layui-word-aux">例:/admin/接口 或 /admin/接口/方法名</div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <input type="text" name="remark" lay-verify="" placeholder="请输入备注" autocomplete="off" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">新建</button>
                    <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
                </div>
            </div>
        </form>
    </div>

    <%- include('script') %>
        <script>
            var formSelects = layui.formSelects;
            var vm = new Vue({
                el: '#app',
                data: function () {
                    return {
                        isButtonDisabled: false,
                    }
                },
                mounted: function () {
                    var self = this;

                    layui.use(['form'], function () {
                        var form = layui.form;

                        //定义验证规则
                        form.verify({
                            path: function(value, item){ //value：表单的值、item：表单的DOM对象
                                if(!new RegExp("^/[a-z]+/([a-z]+|[a-z]+/[a-z]+|[a-z]+/[a-z]+/[a-z]+)$").test(value)){
                                    return '路径必须以\'/\'开头,且只能包含\'/和小写字母\'';
                                }
                            },
                            //我们既支持上述函数式的方式，也支持下述数组的形式
                            //数组的两个值分别代表：[正则匹配、匹配不符时的提示文字]
                            code: [
                                /^[A-Za-z]+$/
                                ,'编号只能包含字母,不能有中文或者下划线等字符'
                            ],
                            name:[/^.{1,20}$/,'只能填写长度为1-20的所有字符']
                        });

                        //监听提交
                        form.on('submit(save)', function (data) {
                            self.isButtonDisabled = true;
                            $.ajax({
                            url:'/admin/auth',
                            type:'POST',
                            data:{data:data.field},
                            success:(res)=>{
                                if (res.code == 200) {
                                    parent.vm.iframesave()
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                    parent.layer.msg('新建成功!',{icon:1})
                                } else {
                                    //icon 1绿√ 2红叉 3问号 4密码锁 5不开心 6开心
                                    layer.msg('新建失败:'+res.error+' -- '+res.code,{icon:2})
                                }
                                self.isButtonDisabled = false;
                            }
                            });
                            return false;
                        });
                    });
                },
                methods: {
                    doclose: function () {
                        parent.vm.iframeclose();
                        var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                        parent.layer.close(index); //再执行关闭
                    },
                }
            })
        </script>
